Fix SecurityError when accessing localStorage in restricted browser environments#1452
Conversation
|
Thanks for your pull request. Is this your first contribution to a Snowplow open source project? Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). 📝 Please visit https://docs.snowplowanalytics.com/docs/contributing/contributor-license-agreement/ to learn more and sign. Once you've signed, please reply here (e.g. I signed it!) and we'll verify. Thanks. |
| try { | ||
| window.localStorage.setItem(queueName, JSON.stringify(events)); | ||
| } catch (e) { | ||
| // Silently fail if localStorage is not accessible |
There was a problem hiding this comment.
Copilot Rather than failing silently, can we here and above in the other catch block call console.error to log the failure?
There was a problem hiding this comment.
Added console.error logging in both catch blocks to help with debugging localStorage access issues. The error messages now indicate:
- "Failed to access localStorage when initializing event store" (line 29)
- "Failed to persist events to localStorage" (line 45)
Co-authored-by: matus-tomlein <42166+matus-tomlein@users.noreply.github.com>
|
Copilot running the tests results in an error: |
Co-authored-by: matus-tomlein <42166+matus-tomlein@users.noreply.github.com>
... Fixed the test to use the proper |
b8e009f
into
release/4.6.7
…nvironments (#1452) * Initial plan * Wrap localStorage access in try-catch blocks to handle SecurityError * Add console.error logging for localStorage access failures Co-authored-by: matus-tomlein <42166+matus-tomlein@users.noreply.github.com> * Fix test to use proper EventStorePayload format Co-authored-by: matus-tomlein <42166+matus-tomlein@users.noreply.github.com> * Run rush change --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: matus-tomlein <42166+matus-tomlein@users.noreply.github.com> Co-authored-by: Matus Tomlein <matus.tomlein@gmail.com>
Fix: Wrap localStorage access in try-catch blocks to prevent app crashes
Problem
The Snowplow JavaScript tracker was directly accessing
window.localStoragewithout proper error handling, which throws aSecurityErrorin certain browser configurations:dom.storage.enableddisabledThis caused the tracker to break the host application.
Solution
Wrapped both localStorage access points in
local_storage_event_store.tswith try-catch blocks:window.localStorage.getItem()- Now wrapped in try-catch, falls back to empty in-memory store on error and logs error to consolewindow.localStorage.setItem()- Now wrapped in try-catch, silently continues with in-memory storage on error and logs error to consoleBehavior After Fix
Testing
Added comprehensive test suite (
test/tracker/local_storage_event_store.test.ts) that verifies:getItem()throws SecurityErrorsetItem()throws SecurityErrorChanges Made:
window.localStorage.getItem()call in try-catch (line 24)window.localStorage.setItem()call in try-catch (line 42)Original prompt
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.